﻿using System;
using System.Linq;
using GdHotel.Data.Interfaces;

namespace GdHotel.Data.Repositories
{
    public class CustomPagesRepository : ICustomPagesRepository
    {
        private GdHotelEntities entities = new GdHotelEntities();

        public IQueryable<CustomPage> FindAll()
        {
            return entities.CustomPages.AsQueryable();
        }

        public IQueryable<CustomPage> FindAllByLang(Language lang)
        {
            return entities.CustomPages.Where(page => page.LanguageId == lang.Id).AsQueryable();
        }

        public IQueryable<CustomPage> FindAllByLangAndKey(Language lang, String key)
        {
            return entities.CustomPages
                .Where(page => page.LanguageId == lang.Id)
                .Where(page => page.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase))
                .AsQueryable();
        }

        public IQueryable<CustomPage> FindAllByLangAndName(Language lang, String name)
        {
            return entities.CustomPages
                .Where(page => page.LanguageId == lang.Id)
                .Where(page => page.StandaloneName.Equals(name, StringComparison.InvariantCultureIgnoreCase))
                .AsQueryable();
        }

        public CustomPage GetById(Int32 id)
        {
            return entities.CustomPages.SingleOrDefault(page => page.Id == id);
        }

        public void Insert(CustomPage page)
        {
            entities.AddToCustomPages(page);
        }

        public void Delete(CustomPage page)
        {
            entities.CustomPages.DeleteObject(page);
        }

        public void Save()
        {
            entities.SaveChanges();
        }

        public void Dispose()
        {
            entities.Dispose();
        }
    }
}
